<?php
namespace App\Datas;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class UserOntrial
{

    public function createData(array $insertData) {
        $id = DB::table('user_ontrial')->insertGetId($insertData);
        return $id;
    }

    //获取适用状态
    public function getStatus($userId)
    {
        $trial = DB::table('user_ontrial')->where(['user_id'=>$userId])->orderBy("id","desc")->get()->toArray();
        if ($trial[0] ?? false) {
            return (array)$trial[0];
        }
        return null;
    }

    //获取适用状态
    public function getStatusByDeviceId($deviceId){
        $trial = DB::table('user_ontrial')->where(['device_id'=>$deviceId])->orderBy("id","desc")->get()->toArray();
        if ($trial[0] ?? false) {
            return (array)$trial[0];
        }
        return null;
    }

    public function updateUserIdByDeviceId($deviceId,$userId) {
        DB::table('user_ontrial')->where(["device_id" => $deviceId,"user_id" => 0])->update(["user_id" => $userId]);
    }

    //检测是否有效
    public function checkValid($ontrialInfo) {
        if (!empty($ontrialInfo) && $ontrialInfo["exper_time"] > time()) {
            return true;
        }
        return false;
    }
}